package com.qf.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateHelper {
	public static String getServerTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	public static String getServerTime(String template) {
		return new SimpleDateFormat(template).format(new Date());
	}

	public static boolean isDateTime(String arg, String template) {
		DateFormat df = new SimpleDateFormat(template);
		try {
			Date date = df.parse(arg);
			return df.format(date).equals(arg);
		} catch (Exception e) {
		}
		return false;
	}

	public String getHexString(String str) {
		String newName = new String();
		for (int i = 0; i < str.length(); i++) {
			if (Character.toString(str.charAt(i)).matches("[0-9a-zA-Z\\._\\-]"))
				newName = newName + str.charAt(i);
			else {
				newName = newName + Integer.toHexString(str.charAt(i));
			}
		}
		return newName;
	}

	public static Date toDate(String str, String pattner) throws ParseException {
		return new SimpleDateFormat(pattner).parse(str);
	}

	public static String formatDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
	/**
	 * 功能：判断字符串是否为日期格式
	 *
	 * @param strDate
	 * @return
	 */
	public static boolean isDate(String strDate) {
		Pattern pattern = Pattern
				.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
		Matcher m = pattern.matcher(strDate);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}
}